#include "str_util.h"

#include <direct.h>
#include <iostream>
#include <iterator>

template <typename T>
static inline void SplitStringToIteratorUsing(const std::string& full, const char* delim, T& result)
{
	if (delim[0] != '\0' && delim[1] == '\0')
	{
		char c = delim[0];
		const char* p = full.data();
		const char* end = p + full.size();
		while (p != end) 
		{
			if (*p == c) {
				++p;
			} else {
				const char* start = p;
				while (++p != end && *p != c);
				*result++ = std::string(start, p - start);
			}	
		}
		return;
	}

	std::string::size_type begin_index, end_index;
	begin_index = full.find_first_not_of(delim);
	while (begin_index != std::string::npos) 
	{
		end_index = full.find_first_of(delim, begin_index);
		if (end_index == std::string::npos)
		{
			*result++ = full.substr(begin_index);
			return;
		}
		*result++ = full.substr(begin_index, (end_index - begin_index));
		begin_index = full.find_first_not_of(delim, end_index);
	}

}

void SplitStringUsing(const std::string& full, const char* delim, std::vector<std::string>* result)
{
	std::back_insert_iterator< std::vector<std::string> > it(*result);
	SplitStringToIteratorUsing(full, delim, it);
}